package com.rjwh.dingdong.client.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Kay
 * @ClassName GameSoundPaly
 * @Description
 * @date 2016-8-19
 * @history 2016-8-19 author: description:
 */
public class GameSoundPaly {

    private static GameSoundPaly gameSoundPaly;


    private HashMap<String,MediaPlayUtil> mapMediaPlay = new HashMap<>();

    private GameSoundPaly() {
    }

    public static synchronized GameSoundPaly getInstance() {
        if (gameSoundPaly == null) {
            gameSoundPaly = new GameSoundPaly();
        }
        return gameSoundPaly;
    }

    public void play(String role, String fileName, boolean isLoop) {

        //存在
        if(mapMediaPlay.containsKey(role)){
            ((MediaPlayUtil)mapMediaPlay.get(role)).playAudio(fileName,isLoop);
        }else{
            MediaPlayUtil mediaPlayUtil = new MediaPlayUtil();
            mapMediaPlay.put(role,mediaPlayUtil);
            mediaPlayUtil.playAudio(fileName,isLoop);
        }
    }

    public void stop(String role) {

        //存在
        if(mapMediaPlay.containsKey(role)){
            ((MediaPlayUtil)mapMediaPlay.get(role)).stop();
        }
    }

    public void pause(String role) {
        //存在
        if(mapMediaPlay.containsKey(role)){
            ((MediaPlayUtil)mapMediaPlay.get(role)).pause();
        }
    }


    public void resume(String role) {
        //存在
        if(mapMediaPlay.containsKey(role)){
            ((MediaPlayUtil)mapMediaPlay.get(role)).resume();
        }
    }


    public void stopAll() {
        Iterator iter = mapMediaPlay.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            MediaPlayUtil val = (MediaPlayUtil) entry.getValue();
            val.stop();
           }
    }

    public void pauseAll() {
        Iterator iter = mapMediaPlay.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            MediaPlayUtil val = (MediaPlayUtil) entry.getValue();
            val.pause();
        }
    }

    public void resumeAll() {
        Iterator iter = mapMediaPlay.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            MediaPlayUtil val = (MediaPlayUtil) entry.getValue();
            val.resume();
        }
    }

    public void onDestroy(){
        stopAll();
        mapMediaPlay.clear();
        System.gc();
    }
}
